<?php
/**
 * 好友管理
 * 
 * @author fanzhanao <fanzhanao@178.com>
 * @version $Id: index.php 11498 2010-10-15 06:08:12Z fanzhanao $
 */

class friends_controller_index extends attention_controller_index
{
	/**
	 * friends_model_data
	 *
	 * @var object
	 */
	private $_mod = null;
	
	/**
	 * 模块缓存前缀
	 *
	 * @var str
	 */
	private $_module = null;
	
	public function __construct($app, $controller)
	{
		parent::__construct($app, $controller);
		$this->_mod = new friends_model_data();
		if (!$this->getUid()) {
			$this->requireLogin();
		}
		$this->_module = $app;
		$this->headerView = $this->viewDir."/sns/_header.phtml";
		$this->frameView = VIEW_PATH.'/sns/index.phtml';
	}
	
	public  function index_action(){
		
	}
	
	/**
	 * 好友列表
	 * 
	 * @display true
	 */
	public function get_list_action()
	{		
		$uid      = $this->getUid();
		$currPage = _('page', 1);
		
		list($data, $this->pageInfo) = MemRow::getPageList($this->_module, $currPage, friends_model_data::PAGE_SIZE, array(
			array($this->_mod, 'getListId', array($uid)), array($this->_mod, 'getRow')));

		if (self::$is_self) {
			if ($remarks = $this->_mod->getRemark(self::$login['uid']))
				foreach ($data as $k => $v)	$data[$k]['remark'] = isset($remarks[$v['uid']]) ? $remarks[$v['uid']] : '';
		}

		$this->ret = $data;

        $this->view = 'get_list';
	}
	
	/**
	 * 我发起的请求列表
	 *
	 * @display true
	 */
	public function get_wait_list_action()
	{
		if (!self::$login) return ;
		$uid = (int) self::$login['uid'];
		$currPage = isset($_GET['page']) ? (int) $_GET['page'] : 1;
		
		$requestUser = $this->_mod->getCancels($uid);
		list($uids, $this->pageInfo) = MemRow::getPageListId($requestUser, $currPage, friends_model_data::PAGE_SIZE);
		$this->ret = friends_util_cache::getUserInfo($uids);
        $this->view = 'get_wait_list';
	}
	
	/**
	 * 我收到的请求列表
	 *
	 * @display true
	 */
	public function get_request_list_action()
	{
		if (!self::$login) return ;
		$uid = (int) self::$login['uid'];
		$currPage = isset($_GET['page']) ? (int) $_GET['page'] : 1;
		
		$requestUser = $this->_mod->getRequests($uid);
		list($uids, $this->pageInfo) = MemRow::getPageListId($requestUser, $currPage, friends_model_data::PAGE_SIZE);
		
		$this->ret = friends_util_cache::getUserInfo($uids);		
	}
	
	/**
	 * 添加好友请求表单
	 *
	 * @display false
	 */
	public function prepare_action()
	{
		$this->_renderJson();
		if (!self::$login) {
			$this->ret = self::failure(friends_util_exception::$errorDescriptions[friends_util_exception::API_EC_FRIEND_LOGIN]);
			return ;
		}
		
		$uid = (int) _('add_uid');
		$hid = (int) self::$login['uid'];//host_id

		try {
			$username = $this->_mod->checkFriend($hid, $uid);
			$this->ret = self::success(array(
				'uid'	   => $uid,
				'username' => $username,
				'the'	   => $this->getThe($uid),
				'avatar'   => member_util_avatar::get_avatar($uid, 'small')
			));
		} catch (Exception $err) {
			$code = $err->getMessage();
			$this->ret = self::failure(friends_util_exception::$errorDescriptions[$code]);
		}
		return ;
	}
	
	/**
	 * 添加好友请求
	 *
	 * @display false
	 */
	public function add_action()
	{
		$this->_renderJson();
		if (!self::$login) {
			$this->ret = self::failure(friends_util_exception::$errorDescriptions[friends_util_exception::API_EC_FRIEND_LOGIN]);
			return ;
		}
		
		$uid = (int) _('add_uid');
		$hid = (int) self::$login['uid'];
		
		try {
			$info = $this->_mod->addFriend($hid, $uid, _('message'));
			if ('add' == $info['type']) {
				$this->ret = self::success(array(
					'uid'		=> $uid,
					'username'	=> $info['username'],
					'host_id'	=> self::$login['uid'],
					'host_name' => self::$login['nickname'],
					'message'	=> _('message'),
					'type'		=> 'add'
				));
				$this->addMessage('你的好友请求已经发送，等待对方答复。');
			} elseif ('bind' == $info['type']) {
				$this->ret = self::success(array(
					'uid'		=> $uid,
					'username'	=> $info['username'],
					'host_id'	=> self::$login['uid'],
					'host_name' => self::$login['nickname'],
					'type'		=> 'bind'
				));
//				feed_util_send::save($hid, self::$login['nickname'], 2, $uid, array('uid' => $uid, 'username' => $info['username']));
				$this->addMessage('你和'.$info['username'].'已成为好友。');
			}
		} catch (Exception $err) {
			$code = $err->getMessage();
			$this->ret = self::failure(friends_util_exception::$errorDescriptions[$code]);
		}
		return;
	}
	
	/**
	 * 同意请求，绑定好友
	 *
	 * @display false
	 */
	public function bind_action()
	{
		$this->_renderJson();
		if (!self::$login) {
			$this->ret = self::failure(friends_util_exception::$errorDescriptions[friends_util_exception::API_EC_FRIEND_LOGIN]);
			return ;
		}
		
		$uid = (int) _('quest_uid');
		$hid = (int) self::$login['uid'];
		
		try {
			$username = $this->_mod->bindFriend($hid, $uid);
			feed_util_send::save($hid, self::$login['nickname'], 2, $uid, array('uid' => $uid, 'username' => $username));
		
			$this->ret = self::success(array(
				'uid'		=> $uid,
				'username'	=> $username,
				'host_id'	=> self::$login['uid'],
				'host_name' => self::$login['nickname'],
				'type'		=> 'bind'
			));
		} catch (Exception $err) {
			$code = $err->getMessage();
			$this->ret = self::failure(friends_util_exception::$errorDescriptions[$code]);
		}
		return;
	}
	
	/**
	 * 拒绝请求
	 *
	 * @display false
	 */
	public function refuse_action()
	{
		$this->_renderJson();
		if (!self::$login) {
			$this->ret = self::failure(friends_util_exception::$errorDescriptions[friends_util_exception::API_EC_FRIEND_LOGIN]);
			return ;
		}
		
		$uid = (int) _('quest_uid');
		$hid = (int) self::$login['uid'];
		
		try {
			$username = $this->_mod->refuseRequest($hid, $uid);
			$this->ret = self::success(array(
				'uid'		=> $uid,
				'username'	=> $username,
				'host_id'	=> $hid,
				'host_name' => self::$login['nickname'],
				'type'		=> 'refuse'
			));
		} catch (Exception $err) {
			$code = $err->getMessage();
			$this->ret = self::failure(friends_util_exception::$errorDescriptions[$code]);
		}
	}
	
	/**
	 * 删除好友
	 *
	 * @display false
	 */
	public function remove_action()
	{
		$this->_renderJson();
		if (!self::$login) {
			$this->ret = self::failure(friends_util_exception::$errorDescriptions[friends_util_exception::API_EC_FRIEND_LOGIN]);
			return ;
		}
		
		$uid = (int) _('quest_uid');
		$hid = (int) self::$login['uid'];
		
		try {
			$this->_mod->removeFriend($hid, $uid);
			$this->ret = self::success();
			$this->addMessage('该记录已删除。');
			
		} catch (Exception $err) {
			$code = $err->getMessage();
			$this->ret = self::failure(friends_util_exception::$errorDescriptions[$code]);
		}
	}
	
	/**
	 * 取消等待确认的好友
	 *
	 * @display false
	 */
	public function remove_invit_action()
	{
		$this->_renderJson();
		if (!self::$login) {
			$this->ret = self::failure(friends_util_exception::$errorDescriptions[friends_util_exception::API_EC_FRIEND_LOGIN]);
			return ;
		}
		
		$uid = (int) _('quest_uid');
		$hid = (int) self::$login['uid'];
		
		try {
			$this->_mod->cancelRequest($hid, $uid);
			$this->ret = self::success();
			$this->addMessage('该记录已删除。');
			
		} catch (Exception $err) {
			$code = $err->getMessage();
			$this->ret = self::failure(friends_util_exception::$errorDescriptions[$code]);
		}
	}
	
	/**
	 * 首页我的好友
	 *
	 * @display false
	 */
	public function embed_action()
	{
		$this->_renderSingle();
		$uid = $this->getUid();
		if($uid){
			$friends = $this->_mod->getFriendList($uid, 18);
			if (self::$is_self) {
				if ($remarks = $this->_mod->getRemark(self::$login['uid']))
					foreach ($friends as $k => $v)	$friends[$k]['remark'] = isset($remarks[$v['id']]) ? $remarks[$v['id']] : '';
			}
			
			$this->ret = $friends;
		}else{
			$this->view = null;
		}
	}
	
	/**
	 * 修改好友备注
	 *
	 * @display false
	 */
	public function remark_action()
	{
		$this->_renderJson();
		if (!self::$login) {
			$this->ret = self::failure(friends_util_exception::$errorDescriptions[friends_util_exception::API_EC_FRIEND_LOGIN]);
			return;
		}

		//当前登录用户
		$uid = self::$login['uid'];

		$friendId = _('fid');
		$remark   = _('remark', '');
		$remark   = strip_tags($remark);
		
		if (!preg_match('/^[a-zA-Z0-9_\x{4e00}-\x{9fa5}]{1,15}$/u', $remark)) {
			$this->ret = self::failure(Error::ERROR_WRONG_PARAMS, '备注只能由1-15位字母,数字,下划线,中文组成');
			return;
		}

		$this->_mod->editRemark($uid, $friendId, $remark);
		$this->ret = self::success(array('remark' => htmlspecialchars($remark)));
	}
}
